﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace IslandInterloper.Engine
{
    internal class AvatarIsland : GameClient
    {

        int id = 0;
        bool visited = false;
        int dayLocated = 0;
        int dayFirstVisited = 0;
        int dayLastVisited = 0;
        int visitCount = 0;
        bool bookMarked = false;
        string notes = string.Empty;

        public int ID
        {
            get
            {
                return id;
            }
        }
        public int DayLocated
        {
            get
            {
                return dayLocated;
            }
        }
        public int DayFirstVisited
        {
            get
            {
                return dayFirstVisited;
            }
        }
        public int DayLastVisited
        {
            get
            {
                return dayLastVisited;
            }
        }
        public bool Visited
        {
            get
            {
                return visited;
            }
        }
        public int VisitCount
        {
            get
            {
                return visitCount;
            }
        }
        public bool BookMarked
        {
            get
            {
                return bookMarked;
            }
        }
        public string Notes
        {
            get
            {
                return notes;
            }
        }
        public void Visit(int theDay)
        {
            visitCount++;
            if (Visited)
            {
                dayLastVisited = theDay;
            }
            else
            {
                visited = true;
                dayFirstVisited = theDay;
                dayLastVisited = theDay;
            }
        }
        public void ToggleBookMark()
        {
            bookMarked = !bookMarked;
        }
        public void Annotate(string theNotes)
        {
            notes = theNotes;
        }
        internal AvatarIsland(Game theGame, int theId, int theDayLocated)
            : base(theGame)
        {
            id = theId;
            dayLocated = theDayLocated;
        }
        internal AvatarIsland(Game theGame, XElement element)
            : base(theGame)
        {
            FromXElement(element);
        }
        private void FromXElement(XElement element)
        {
            dayLocated = Utilities.XElementToInt(element.Element(GameXNames.dayLocated));
            visited = Utilities.XElementToBool(element.Element(GameXNames.visited));
            if (Visited)
            {
                id = Utilities.XElementToInt(element.Element(GameXNames.id));
                dayFirstVisited = Utilities.XElementToInt(element.Element(GameXNames.dayFirstVisited));
                dayLastVisited = Utilities.XElementToInt(element.Element(GameXNames.dayLastVisited));
                visitCount = Utilities.XElementToInt(element.Element(GameXNames.visitCount));
                bookMarked = Utilities.XElementToBool(element.Element(GameXNames.bookmarked));
                notes = element.Element(GameXNames.notes).Value;
            }
            else
            {
                id = Game.Config.NullIslandId;
                visitCount = 0;
                dayFirstVisited = 0;
                dayLastVisited = 0;
                bookMarked = false;
                notes = string.Empty;
            }
        }
        public override XElement ToXElement(XName xName)
        {
            XElement result = new XElement(xName,
                new XElement(GameXNames.dayLocated, dayLocated),
                new XElement(GameXNames.visited, visited)
                );
            if (Visited)
            {
                result.Add(
                    new XElement(GameXNames.id, ID),
                    new XElement(GameXNames.dayFirstVisited, DayFirstVisited),
                    new XElement(GameXNames.dayLastVisited, DayLastVisited),
                    new XElement(GameXNames.visitCount, visitCount),
                    new XElement(GameXNames.bookmarked, bookMarked),
                    new XElement(GameXNames.notes, notes)
                    );
            }
            return result;
        }

    }
}
